<?php
namespace app\deal\controller;
use think\Controller;
use think\Input;
use think\Db;
use think\Session;
use app\deal\model\Order;
class Index extends Controller{
	public function initialize(){
		parent::initialize();
		$uid = Order::getUserId();
		if(!$uid){
			$this->redirect('/user/login/index');
		}
	}
	public function index(){/*
		$this->assign('title','会员中心');
		//获取所有的用户信息
		$user=db('users')->where('id='.$_SESSION['think']['user']['id'])->find();
		$lname=db('user_level')->where('level',$user['level'])->value('level_name');
		$is_wang=db('users')->where(array('uid'=>$_SESSION['think']['user']['id'],'is_b'=>0))->count();
		$sys=getsystem();
		$this->assign('user',$user);
		$this->assign('tuijian',$sys['tuijian']);
		$this->assign('wlt',$sys['wlt']);
		$this->assign('is_wang',$is_wang);
		$this->assign('lname',$lname);*/
		session_start();
		$data = Order::get_index();
		$this->assign('data',$data);
		// echo json_encode($_SESSION['think']['user']['id']);
		return $this->fetch();
	}
	public function deal_way(){

		return $this->fetch();

	}
	//买入订单列表
	public function buy(){

		return $this->fetch();

	}
	//卖出订单列表
	public function sell(){

		return $this->fetch();

	}
	//购置订单记录
	public function order(){

		return $this->fetch();

	}
	//收款方式
	public function payment_term(){
		$data = Order::payment_term();

		$this->assign('data',$data);

		return $this->fetch();

	}//新增付款方式
	public function add_term(){

		return $this->fetch();

	}
	//订单列表详细
	public function order_info(){
		$data = Order::get_order_info(input('id'));

		$this->assign('data',$data);

		return $this->fetch();

	}
	//买单下单
	public function buy_order(){

		echo json_encode(Order::buy_order(input()));exit;

	}
	//买单下单
	public function sell_order(){

		echo json_encode(Order::sell_order(input()));exit;

	}
	//获取买单列表
	public function get_buy_order(){

		$info = input();
		$info['b_or_s'] = 1;
		echo json_encode(Order::get_order($info));exit;

	}
	//获取卖单列表
	public function get_sell_order(){
		$info = input();
		$info['b_or_s'] = 2;
		echo json_encode(Order::get_order($info));exit;

	}
	//用户匹配订单
	public function buy_order_p(){

		echo json_encode(Order::buy_order_p(input()));exit;

	}
	//用户买单列表
	public function get_user_buy(){
		$info = input();
		$info['b_or_s'] = 1;
		echo json_encode(Order::get_user_order($info));exit;

	}
	public function get_user_sell(){
		$info = input();
		$info['b_or_s'] = 2;
		echo json_encode(Order::get_user_order($info));exit;

	}
	//买单取消
	public function cancel_buy(){

		echo json_encode(Order::cancel_buy(input('id')));exit;

	}
	//买单取消
	public function cancel_sell(){

		echo json_encode(Order::cancel_sell(input('id')));exit;

	}
	//获取当前订单
	public function get_user_order(){

		echo json_encode(Order::get_user_bsorder(input('num')));exit;

	}
	//删除收款方式
	public function term_del(){

		Db::name('bs_receipt')->where(array('id'=>input('id')))->update(array('is_true'=>1));

	}
	public function term_up(){

		Db::name('bs_receipt')->where(array('id'=>input('id')))->update(array('state'=>input('ischecked')));

	}
	//上传收据截图
	public function affirm_pay(){

		echo json_encode(Order::affirm_pay(input()));exit;

	}
	//确认收款
	public function order_receipt(){

		echo json_encode(Order::order_receipt(input()));exit;

	}
	//添加收款方式
	public function addterm(){

		echo json_encode(Order::addterm(input()));exit;

	}
	public function uploadImage(){

		//bootstrap-fileinput
		if (request()->isAjax())
		{

			// 获取表单上传文件 例如上传了001.jpg
			$file = request()->file('file');

			//判断文件是否存在
			if (empty($file))
			{
				echo json_encode(array("status"=>2,"error"=>"上传图片为空(不可为中文名字)！"));exit;
			}

			$root_path_xf = $_SERVER['DOCUMENT_ROOT'];	
			//保存路径
			$savePath = $root_path_xf.'/uploads/images';

			$result = $file
				->rule("upload_save_name")
				->validate(array("ext"=>"jpg,png,gif,jpeg"))
				->move($savePath);

			if ($result)
			{
				$imgPath = "/uploads/images" . '/' . str_replace("\\","/",$result->getSaveName());
				$urls = "http".(((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 's':"").'://'.$_SERVER['SERVER_NAME'];
				echo json_encode(array("status"=>0,"url"=>$urls.$imgPath));exit;
			}
			else
			{
				return array("status"=>2,"error"=>"图片上传失败！");
			}
		}

	}

}